package cn.hncu.booksManagementSystem.user.business.ebo;

import java.util.Collection;

import cn.hncu.booksManagementSystem.user.vo.UserModel;
import cn.hncu.booksManagementSystem.common.enums.UserTypeEnum;
import cn.hncu.booksManagementSystem.user.business.ebi.UserEbi;
import cn.hncu.booksManagementSystem.user.dao.dao.UserDAO;
import cn.hncu.booksManagementSystem.user.dao.factory.UserDAO_Factory;
import cn.hncu.booksManagementSystem.user.vo.UserQueryModel;

public class UserEbo implements UserEbi{
	//注入dao
	private UserDAO dao = UserDAO_Factory.getInstancnOfUserDAO();
	@Override
	public UserModel login(UserQueryModel uqm) {
		
		Collection<UserModel> col = dao.getUsersByCondition(uqm);
		for (UserModel user : col) {
			if (user.getName().equals(uqm.getName())&&user.getPwd().equals(uqm.getPwd())) {
				return user;
			}
		}
		return null;
	}
	
	@Override
	public Collection<UserModel> getAllUser() {
		return dao.getAll();
	}

	@Override
	public boolean createUser(UserModel user) {
		return dao.add(user);
	}

	@Override
	public boolean deleteUser(UserModel user) {
		return dao.delete(user);
	}

	@Override
	public boolean updateUserInfo(UserModel newUser) {
		return dao.update(newUser);
	}

	@Override
	public Collection<UserModel> getUsersByCondition(UserQueryModel uqm) {
		return dao.getUsersByCondition(uqm);
	}

	@Override
	public UserModel getUserById(String id) {
		return dao.getUserModelById(id);
	}

	@Override
	public Collection<UserModel> getAllInAndAdmin() {
		UserQueryModel uqm = new UserQueryModel();
		uqm.setType(UserTypeEnum.IN.getType());
		Collection<UserModel> resCol = getUsersByCondition(uqm);
		uqm.setType(UserTypeEnum.ADMIN.getType());
		resCol.addAll(getUsersByCondition(uqm));
		return resCol;
	}

	@Override
	public Collection<UserModel> getAllOutAndAdmin() {
		UserQueryModel uqm = new UserQueryModel();
		uqm.setType(UserTypeEnum.OUT.getType());
		Collection<UserModel> resCol = getUsersByCondition(uqm);
		uqm.setType(UserTypeEnum.ADMIN.getType());
		resCol.addAll(getUsersByCondition(uqm));
		return resCol;
	}

}
